package teehee.UI;

import java.util.ArrayList;

import teehee.Form.HealthForm;
import teehee.Manager.HealthManager;
import teehee.homepage.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author Bohan Liu
 * note: this document assumes there would be no null input
 */

/*
 * Remaining tasks: 
 * 1.recalculate BMI when user press generate history with new height. 
 * 2. reverse the print order.
 * 
 */

public class HealthHistory extends Activity {
	//String weight;
	static ArrayList<String> weight_data = new ArrayList<String>();
	static ArrayList<String> height_data = new ArrayList<String>();
	static ArrayList<String> date_data = new ArrayList<String>();
	static ArrayList<String> bmi_data = new ArrayList<String>();
	static int index = 0;
	static double bmival = 0;
	static double weightval = 0;
	static double heightval = 0;
	public static String weight;
	public static String date;
	public static String height;
	public static String bmi; 
	Button graph;
	public void onCreate(Bundle savedInstanceState) {	

		//static String curHeight;

		super.onCreate(savedInstanceState);
		setContentView(R.layout.health_history);

		Button generate = (Button) findViewById(R.id.generate);
		generate.setOnClickListener( new ButtonClickHandler()  ); 



		graph = (Button) findViewById(R.id.healthHistory_graphButton); 
		graph.setVisibility(Button.GONE);
		/*graph.setOnClickListener(new OnClickListener() {
	    	public void onClick(View v) {
	    		Intent graphPage = new Intent(HealthHistory.this, WeightGraph.class);
	    		startActivity(graphPage);
	    	}
	    });*/

		Context ctx = getApplicationContext();
		String ht = HealthManager.getHeight(ctx);
		View header = findViewById(R.id.header);
		View tableHeader = findViewById(R.id.tableHeader);
		LinearLayout gen = (LinearLayout)findViewById(R.id.genLayout);
		TableLayout layout = (TableLayout)findViewById(R.id.table_health_history);
		layout.removeAllViews();
		layout.addView(gen);
		layout.addView(header);
		layout.addView(tableHeader);
		if(HealthForm.isValid(ht))
		{
			//HealthHistory hlt = new HealthHistory();
			//HealthHistory.generateHistory("72", weight, date);



			for(int i=0; i<HealthManager.getSize(); i++)
			{

				String[] arg = HealthManager.getData(i);
				weight = arg[0];
				date = arg[1];
				if(HealthForm.isValid(weight))
				{
					bmi = HealthManager.generateHistory(ht, weight);
					//this.addNewHistory(weight_data[i], bmi_data[i], date_data[i]); 

					TableLayout table = (TableLayout) findViewById(R.id.table_health_history);
					TableLayout table2 = (TableLayout) findViewById(R.id.table_health_history);

					TableRow tr = new TableRow(HealthHistory.this);

					TextView tvLeft = new TextView(HealthHistory.this);
					tvLeft.setText(date);
					tvLeft.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
					tvLeft.setTextSize(15);
					tvLeft.setTextColor(Color.parseColor("#093055"));

					TextView tvCenter = new TextView(HealthHistory.this);
					tvCenter.setText(weight);
					tvCenter.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
					tvCenter.setTextSize(15);
					tvCenter.setTextColor(Color.parseColor("#093055"));

					TextView tvRight = new TextView(HealthHistory.this);
					tvRight.setText(bmi);
					tvRight.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
					tvRight.setTextSize(15);
					tvRight.setTextColor(Color.parseColor("#093055"));   

					tr.addView(tvLeft);
					tr.addView(tvCenter);
					tr.addView(tvRight);


					TableRow tr2 = new TableRow(HealthHistory.this);

					TextView tvEmpty = new TextView(HealthHistory.this);
					tvEmpty.setText("                 ");
					tvEmpty.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
					tvEmpty.setTextSize(20);
					tvEmpty.setTextColor(Color.parseColor("#093055"));

					tr.addView(tvEmpty);

					table2.addView(tr2);
					table.addView(tr);
				}
			}
		}
	}


	/*
	 *update height here.
	 */


	public class ButtonClickHandler implements View.OnClickListener {
		public void onClick( View view ) {
			EditText EditHeight = (EditText) findViewById(R.id.enterHeight);
			String curHeight = EditHeight.getText().toString().trim();
			if(HealthForm.validHeight(curHeight))
			{
				height = curHeight;
				LinearLayout gen = (LinearLayout)findViewById(R.id.genLayout);
				gen.setVisibility(LinearLayout.GONE);
				View header = findViewById(R.id.header);
				View tableHeader = findViewById(R.id.tableHeader);
				TableLayout layout = (TableLayout)findViewById(R.id.table_health_history);
				layout.removeAllViews();
				layout.addView(header);
				layout.addView(tableHeader);

				HealthManager.updateHeight(curHeight, getApplicationContext());

				for(int i=0; i<HealthManager.getSize(); i++)
				{
					String[] arg = HealthManager.getData(i);
					weight = arg[0];
					date = arg[1];
					if(HealthForm.isValid(weight))
					{
						bmi = HealthManager.generateHistory(height, weight);
						TableLayout table = (TableLayout) findViewById(R.id.table_health_history);
						TableLayout table2 = (TableLayout) findViewById(R.id.table_health_history);

						TableRow tr = new TableRow(HealthHistory.this);

						TextView tvLeft = new TextView(HealthHistory.this);
						tvLeft.setText(date);
						tvLeft.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
						tvLeft.setTextSize(15);
						tvLeft.setTextColor(Color.parseColor("#093055"));

						TextView tvCenter = new TextView(HealthHistory.this);
						tvCenter.setText(weight);
						tvCenter.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
						tvCenter.setTextSize(15);
						tvCenter.setTextColor(Color.parseColor("#093055"));

						TextView tvRight = new TextView(HealthHistory.this);
						tvRight.setText(bmi);
						tvRight.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
						tvRight.setTextSize(15);
						tvRight.setTextColor(Color.parseColor("#093055"));   

						tr.addView(tvLeft);
						tr.addView(tvCenter);
						tr.addView(tvRight);


						TableRow tr2 = new TableRow(HealthHistory.this);

						TextView tvEmpty = new TextView(HealthHistory.this);
						tvEmpty.setText("                 ");
						tvEmpty.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
						tvEmpty.setTextSize(20);
						tvEmpty.setTextColor(Color.parseColor("#093055"));

						tr.addView(tvEmpty);

						table2.addView(tr2);
						table.addView(tr);
					}
				}
				/*Intent addNewWOD = new Intent(HealthHistory.this, HealthHistory.class);
		      startActivity(addNewWOD);*/
				// }
			}
			
			else
			{
				Context context = getApplicationContext();
				Toast toast = Toast.makeText(context, "Height not correct", Toast.LENGTH_LONG);
				toast.setGravity(Gravity.TOP|Gravity.LEFT, 25, 300);
				toast.show();
			}
		}



	}





	@Override 
	public boolean onKeyDown(int keyCode, KeyEvent event) 
	{
		if ((keyCode == KeyEvent.KEYCODE_MENU))
		{ 
			//setContentView(R.layout.main);
			Intent homep = new Intent(HealthHistory.this, MainPage.class);
			startActivity(homep);
			return true;
		}

		if ((keyCode == KeyEvent.KEYCODE_SEARCH))
		{ 
			//setContentView(R.layout.main);
			Intent homep = new Intent(HealthHistory.this, SearchPage.class);
			startActivity(homep);
			return true;
		}

		return super.onKeyDown(keyCode, event);    
	} 
}
